package com.vspush.udp.handle;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;

import java.net.InetSocketAddress;

import com.vspush.cache.Cache;
import com.vspush.manage.MessageHeadle;
import com.vspush.manage.StartMessge;
import com.vspush.udp.model.LinkUser;
import com.vspush.udp.model.User;

public class StartHeadle  extends MessageHeadle{
	
	StartMessge sm = new StartMessge();

	public StartHeadle(String msg) {
		super(msg);
	}

	@Override
	public void headle(ChannelHandlerContext ctx, DatagramPacket msg) {
		boolean isu = false;
		LinkUser u  =sm.getUserbyMsg(this.msg);
		if(u!=null){
			isu = true;
			InetSocketAddress masg = msg.sender();
			masg.getAddress().getHostAddress();
			String name =u.getUsername();
			if(!Cache.isHas(name)){
				User u_ = new User();
				u_.setIsa(masg);
				u_.setName(name);
				Cache.put(name, u_, 120);
			}
		}
		if(!isu){
			sm.startError(msg.sender());
		}else{
			sm.startOk(msg.sender());
		}
	}

}
